package com.ruoyi.web.controller.biz;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.BsUserContract;
import com.ruoyi.system.service.BsUserContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @Package: com.ruoyi.web.controller.biz
 * @Author: HanWL
 * @CreateTime: 2023-10-18 14:30
 * @Version: 1.0
 **/
@RestController
@Api(tags = "用户联系人接口")
@RequestMapping("/bs/user/contract")
public class BsUserContractController extends BaseController {

    @Resource
    private BsUserContractService bsUserContractService;

    @ApiOperation("查询我的所有联系人")
    @GetMapping("/list")
    public AjaxResult<List<BsUserContract>> list(){
        LambdaQueryWrapper<BsUserContract> lqw = new LambdaQueryWrapper<>();
        lqw.eq(BsUserContract::getUserId,SecurityUtils.getUserId());
        List<BsUserContract> list = bsUserContractService.list(lqw);
        return AjaxResult.success(list);
    }

    @ApiOperation("新增联系人")
    @PostMapping("/save")
    public AjaxResult<?> list(@RequestBody BsUserContract bsUserContract){
        bsUserContract.setUserId(SecurityUtils.getUserId());
        if(bsUserContractService.checkUserExist(bsUserContract.getContractCard(),bsUserContract.getUserId())){
            return AjaxResult.error("该用户已存在！");
        }
        boolean result = bsUserContractService.save(bsUserContract);
        return result ? AjaxResult.success(bsUserContract.getContractId()) : AjaxResult.error();
    }

    @ApiOperation("修改联系人")
    @PutMapping("/update")
    public AjaxResult<Void> update(@RequestBody BsUserContract bsUserContract){
        boolean result = bsUserContractService.updateById(bsUserContract);
        return toAjax(result);
    }

    @ApiOperation("删除联系人")
    @DeleteMapping("/delete/{ids}")
    public AjaxResult<Void> delete(@PathVariable Long[] ids){
        boolean result = bsUserContractService.removeByIds(Arrays.asList(ids));
        return toAjax(result);
    }
}
